package ru.marketsoft.reminder.frames;

import ru.marketsoft.reminder.tools.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * User: dimka3210
 * Date: 14.03.13
 * Time: 22:22
 */
public class SettingsFrame extends JDialog {

    private JTextField reportSite;
    private JComboBox comboBox1;
    private JComboBox comboBox2;
    private JTextField messageField;
    private JLabel messageText;
    private JButton okButton;
    private JButton cancelButton;
    private JPanel mainPanel;
    private JComboBox comboBox3;
    private JButton playButton;
    private static SettingsFrame instance = null;

    public static SettingsFrame getInstance() {
        if (instance == null) {
            instance = new SettingsFrame();
        }

        return instance;
    }

    public SettingsFrame() {
        super();
        setTitle("Установки");
        setContentPane(mainPanel);
        setIconImage(Utils.getIcon());
        pack();
        Dimension useScreen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((int) (useScreen.getWidth() / 2 - getWidth() / 2), (int) (useScreen.getHeight() / 2 - getHeight() / 2));

        this._preView();

        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Settings.getInstance().setProperty("report_site", reportSite.getText());
                Settings.getInstance().setProperty("message", messageField.getText());
                Settings.getInstance().setProperty("time.hours", comboBox1.getSelectedItem().toString());
                Settings.getInstance().setProperty("time.minutes", comboBox2.getSelectedItem().toString());
                try {
                    Settings.getInstance().setProperty("sound_item", String.valueOf(comboBox3.getSelectedItem().toString()));
                } catch (NullPointerException e1) {
                    Settings.getInstance().setProperty("sound", "off");
                }
                setVisible(false);
            }
        });
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                reportSite.setText(Settings.getInstance().getProperty("report_site"));
                messageField.setText(Settings.getInstance().getProperty("message"));
                comboBox1.setSelectedIndex(Integer.parseInt(Settings.getInstance().getProperty("time.hours")));
                comboBox2.setSelectedIndex(Integer.parseInt(Settings.getInstance().getProperty("time.minutes")));
                comboBox3.setSelectedItem(Settings.getInstance().getProperty("sound_item"));
                setVisible(false);
            }
        });
        playButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File forPath = new File("ru/marketsoft/reminder/sounds/" + comboBox3.getSelectedItem().toString());
                String soundSelectedPath = forPath.getAbsolutePath();
                AePlayWave aw = new AePlayWave(soundSelectedPath);
                aw.start();
            }
        });
    }

    private void _preView(){
        try {
            reportSite.setText(Settings.getInstance().getProperty("report_site"));
            messageField.setText(Settings.getInstance().getProperty("message"));
            comboBox1.setSelectedIndex(Integer.parseInt(Settings.getInstance().getProperty("time.hours")));
            comboBox2.setSelectedIndex(Integer.parseInt(Settings.getInstance().getProperty("time.minutes")));

            File[] fileList = new File("ru/marketsoft/reminder/sounds").listFiles();

            for (int i = 0; i < fileList.length; i++) {
                comboBox3.insertItemAt(fileList[i].getName(), i);
            }

            comboBox3.setSelectedItem(Settings.getInstance().getProperty("sound_item"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void setVisible(boolean state){
        this._preView();
        super.setVisible(state);
    }
}
